﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_Activity_ProcessRole : BaseUserControl
{
    public EventHandler OnProcessedSelectRole;
    public EventHandler OnCancelSelectRole;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void InitData(string instanceID, string activityState)
    {
        ViewState["InstanceID"] = instanceID;
        ViewState["ActivityState"] = activityState;
        string cmdText = @"select * from (
	                select a.UserID, a.Username, a.Status, a.IsStat,
	                (select Subject from T_OrgTree where OrgID=b.OrgID and Category='{0}') OrgName,
                    (select QueryCode from T_OrgTree where OrgID=b.OrgID and Category='{0}') QueryCode  
	                from PMS_User a left join T_OrgUser b on a.UserID=b.UserID where OrgID is not null
                )
                m
                right join (
	                select a.Subject RoleSubject, b.UserID from T_SecurityRole a left join T_SecurityPermission b
	                on a.RoleID=b.RoleID where a.Description='{1}'
                )n
                on m.UserID=n.UserID where m.UserID is not null order by m.QueryCode";

        cmdText = string.Format(cmdText, OrgTreeAccess.TREE_XINGZHENG, activityState);

        DataSet ds = BaseAccess.GetDataSet(base.Connection, cmdText);
        gridViewSelect.DataSource = ds.Tables[0];
        gridViewSelect.DataBind();
    }


    /// <summary>
    /// 从ProcessList页面近来的
    /// </summary>
    /// <param name="instanceID"></param>
    /// <param name="activityState"></param>
    public void InitDataByProcess(string instanceID, string activityState, string activityID)
    {
        InitData(instanceID, activityState);
        ViewState["activityID"] = activityID;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["UserID"]))
        {
            base.Alert("请选择一条记录");

            InitData(ViewState["InstanceID"].ToString(), ViewState["ActivityState"].ToString());
            return;
        }

        string cmdText = "delete from T_WorkflowProcess where InstanceID='{0}' and UserID='{1}' and  ActivityState='{2}'";
        cmdText = string.Format(cmdText, ViewState["InstanceID"].ToString(), Request["UserID"], ViewState["ActivityState"].ToString());
        BaseAccess.ExecuteNonQuery(base.Connection, cmdText);

        DataSet ds = BaseAccess.GetDataSet(base.Connection,
            "select * from T_WorkflowProcess where 1=2", "T_WorkflowProcess");
        DataRow row = ds.Tables[0].NewRow();
        row["ActivityID"] = Guid.NewGuid().ToString();
        row["InstanceID"] = ViewState["InstanceID"].ToString();
        row["UserID"] = Request["UserID"];
        row["IsProcess"] = 0;
        row["ActivityState"] = ViewState["ActivityState"].ToString();
        row["CreateDate"] = DateTime.Now.ToString();
        row["Description"] = string.Empty;
        ds.Tables[0].Rows.Add(row);
        BaseAccess.UpdateDataSet(base.Connection, "T_WorkflowProcess", ds);

        if (ViewState["activityID"] == null)
        {
            //从工作流填报页面近来的
            cmdText = "update T_Workflow set IsSubmit=1 where InstanceID='{0}'";
            cmdText = string.Format(cmdText, ViewState["InstanceID"].ToString());
            BaseAccess.ExecuteNonQuery(base.Connection, cmdText);
        }
        else
        {
            //从ProcessList页面近来的
            cmdText = "update T_WorkflowProcess set IsProcess=1 where activityID='{0}'";
            cmdText = string.Format(cmdText, ViewState["activityID"].ToString());
            BaseAccess.ExecuteNonQuery(base.Connection, cmdText);
        }
        if (OnProcessedSelectRole != null) OnProcessedSelectRole(sender, e);
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (OnCancelSelectRole != null) OnCancelSelectRole(sender, e);
    }
}
